package com.sdyd.antivirus.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.utils.UUIDUtils;

public class QuestionInfo {
    private String id;

    private String name;

    private String need;

    private String type;

    private String options;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getNeed() {
        return need;
    }

    public void setNeed(String need) {
        this.need = need == null ? null : need.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type == null ? null : type.trim();
    }

    public String getOptions() {
        return options;
    }

    public void setOptions(String options) {
        this.options = options == null ? null : options.trim();
    }

    public JSONObject toJson(){
        JSONObject obj = new JSONObject();
        obj.put("id", id);
        obj.put("title", name);
        obj.put("type", type);
        boolean isNeed = need != null && need.equals("1");
        obj.put("need", isNeed);
        if (options != null) {
            JSONArray array = JSONArray.parseArray(options);
            if (array.size() > 0) {
                obj.put("options", array);
            }
        }

        return obj;
    }

    public static QuestionInfo allocByJson(JSONObject obj){
        QuestionInfo info = new QuestionInfo();
        info.setName(obj.getString("title"));
        info.setType(obj.getString("type"));
        if (obj.getBoolean("need") == true) {
            info.setNeed("1");
        }
        else {
            info.setNeed("0");
        }
        info.setOptions(obj.getString("options"));

        if (info.getName() == null || info.getType() == null) {
            return null;
        }

        info.setId(UUIDUtils.getUUID());
        return info;
    }
}